package com.ele.dao.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanHandler;
import com.ele.dao.BusinessDao;
import com.ele.pojo.Business;
import com.ele.utils.JDBCUtils;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.*;

public class BusinessDaoImpl implements BusinessDao {

    @Override
    public Integer BusinessRegister(Business business) throws Exception {
        DataSource datasource = JDBCUtils.getDatasource();
        business.setRegTime(new Date());
        return Db.use(datasource).insert(
                Entity.create("business")
                        .set("account",business.getAccount())
                        .set("password",business.getPassword())
                        .set("telephone",business.getTelephone())
                        .set("contact",business.getContact())
                        .set("reg_time",business.getRegTime()));
    }

    @Override
    public Business businessLogin(String account, String password) throws Exception {
        DataSource datasource = JDBCUtils.getDatasource();
        return  Db.use(datasource).query("select * from business where account = ? and password = ?",
                BeanHandler.create(Business.class),account,password);
    }

    @Override
    public Integer businessUpdate(Business business) throws Exception {
        DataSource datasource = JDBCUtils.getDatasource();
        return Db.use(datasource).update(Entity.create()
                .set("account",business.getAccount())
                .set("password",business.getPassword())
                .set("telephone",business.getTelephone())
                .set("contact",business.getContact()),Entity.create("business").set("id",business.getId()));
    }

    @Override
    public Integer businessDelete(String id) throws Exception {
        DataSource datasource = JDBCUtils.getDatasource();
        return Db.use(datasource).del("business","id",id);
    }

    @Override
    public List<Business> findAll(String account , String contact, String telephone,String currentPage,String pageSize) throws Exception {
        DataSource datasource = JDBCUtils.getDatasource();
        String sql = "select * from business ";
        // 定义一个Map集合，将搜索条件对应的列名作为map的key，搜索条件的值作为map的value
        Map<String ,String> map = new HashMap<String ,String>();
        // 判断传递的参数
        if(StrUtil.isNotBlank(account)){
            map.put("account",account);
        }
        if(StrUtil.isNotBlank(contact)){
            map.put("contact",contact);
        }
        if(StrUtil.isNotBlank(telephone)){
            map.put("telephone",telephone);
        }
        // 这里可以直接判断map是否位空
        if(!map.isEmpty()){
            sql = sql + " where ";
            // 判断成立，说明map中有搜索的条件数据，这时只需要遍历map，去给sql中拼接条件即可
            Set<Map.Entry<String, String>> entrySet = map.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                sql = sql + entry.getKey() +" like " + "'%"+ entry.getValue()+"%' or ";
            }
            // sql的最后多or 关键字，需要单独处理  "select * from business where account like %zhang% or contact like %100% or"
            sql = sql.substring(0,sql.lastIndexOf("or"));
        }
        // "select * from business where account like %zhang% or contact like %100% limit 起始位置 , 条数"
        // 判断分页的参数是否携带
        Integer page = 1;
        Integer size = 2;
        if(StrUtil.isNotBlank(currentPage)){
            page = Integer.valueOf(currentPage);
        }
        if( StrUtil.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }
        sql = sql + "limit "+(page - 1) * size +" , " + size;

        return Db.use(datasource).query(sql, Business.class);
    }
    public Long selectCount() throws SQLException {
        DataSource datasource = JDBCUtils.getDatasource();
//        String sql = "select count(*) from business";
//        Connection conn = dataSource.getConnection();
//        PreparedStatement stmt = conn.prepareStatement(sql);
//        ResultSet rs = stmt.executeQuery();
//        if(rs.next()){
//            return rs.getLong(1);
//        }
//        return null;
        return Db.use(datasource).count(Entity.create("business"));
    }
}
